import React from 'react';
import { useNavigate } from 'react-router-dom/dist';
import { Button, Form, Input, message } from 'antd';
import styled from './login.module.css';
import { login } from '@/api/login';
import { loginReturn } from './types';
import { IResponse } from '@/types';

type FieldType = {
    username?: string;
    password?: string;
};
export default function Login() {
    const nav = useNavigate();
    const onFinish = (values: any) => {
        login<loginReturn>(values).then((res) => {
            if (res.statusCode === 200) {
                localStorage.setItem('token', res.access_token);
                nav('/goods');
            } else {
                message.error(res.message);
            }
        });
    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };

    return (
        <div className={styled.login}>
            <Form
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                style={{ maxWidth: 600 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item<FieldType>
                    label="Username"
                    name="username"
                    rules={[
                        {
                            required: true,
                            message: '用户名最小为4位最大为24位',
                            min: 4,
                            max: 24,
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item<FieldType>
                    label="Password"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                            validator(rule, value, callback) {
                                const res = /\d/.test(value) && /[a-zA-Z]/.test(value);
                                if (value.length < 6) {
                                    callback('密码长度不能小于6位');
                                }
                                if (value.length > 24) {
                                    callback('密码长度不能大于24位');
                                }
                                if (!res) {
                                    callback('密码必须包含数字和字母');
                                }
                                callback();
                            },
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        Submit
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
}
